﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Maltose.Configuration;
using MailKit.Net.Smtp;
using MimeKit;

namespace Maltose.Email
{
    /// <summary>
    /// 邮件发送 
    /// </summary>
    public class UseEmail
    {

        /// <summary>
        /// 主机上的端口号。默认值为 25
        /// </summary>
        public int Port { get; set; } = 25;
        /// <summary>
        /// 是否使用安全套接字层 (SSL) 加密连接 默认 true
        /// </summary>
        public bool EnableSsl { get; set; } = true;

        /// <summary>
        /// 获取或设置用于 SMTP 事务的主机的名称或 IP 地址。
        /// </summary>
        public string Host { get; set; } = "";

        /// <summary>
        /// 获取或设置指示邮件正文是否为 Html 格式的值。 默认 true
        /// </summary>
        public bool IsBodyHtml { get; set; } = true;

        /// <summary>
        /// 消息内容
        /// </summary>
        MimeMessage _MailMessage = new MimeMessage();


        /// <summary>
        /// 发送邮箱地址
        /// </summary>
        private string FormEmail { get; set; }

        /// <summary>
        /// 授权码
        /// </summary>
        public string AuthCode { get; private set; }

        /// <summary>
        /// 发送邮件
        /// </summary>
        public UseEmail()
        {

        }

        /// <summary>
        /// 邮件发送 并且设置主机端口
        /// </summary>
        /// <param name="host">主机</param>
        /// <param name="port">端口</param>
        public UseEmail(string host, int port)
        {
            Host = host;
            Port = port;
        }


        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public UseEmail SetConfig(string key)
        {
            //Host = UseConfigFactory.Json.Get<string>($"{key}:Host");
            //Port = UseConfigFactory.Json.Get<int>($"{key}:Port");

            //var fromEmail = UseConfigFactory.Json.Get<string>($"{key}:From");
            //var fromAuthCode = UseConfigFactory.Json.Get<string>($"{key}:AuthCode");
            //var toEmail = UseConfigFactory.Json.Get<string>($"{}:To");

            //From(fromEmail, fromAuthCode);
            //To("");
            //CC("");

            return this;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        public EmailResult Send()
        {
            try
            {
                using (var client = new SmtpClient())
                {
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                    client.Connect(Host, Port, EnableSsl);
                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                    client.Authenticate(FormEmail, AuthCode);
                    client.Send(_MailMessage);
                }
                return new EmailResult();
            }
            catch (Exception e)
            {
                return new EmailResult() { State = false, Msg = e.Message };
            }

        }

        /// <summary>
        /// 发送邮箱人的地址
        /// </summary>
        /// <param name="mailAddress">发件人的地址</param>
        /// <param name="authCode">授权码</param>
        /// <returns></returns>
        public UseEmail From(MailboxAddress mailAddress, string authCode)
        {
            FormEmail = mailAddress.Address;
            AuthCode = authCode;
            mailAddress.Encoding = Encoding.UTF8;
            _MailMessage.From.Add(mailAddress);
            return this;
        }

        /// <summary>
        /// 发送邮箱人的地址
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <param name="authCode">授权码</param>
        /// <returns></returns>
        public UseEmail From(string email, string authCode)
        {
            return From(new MailboxAddress("", email), authCode);
        }

        /// <summary>
        /// 接受人的邮箱地址
        /// </summary>
        /// <param name="mailAddress">邮箱地址</param>
        /// <returns></returns>
        public UseEmail To(MailboxAddress mailAddress)
        {
            _MailMessage.To.Add(mailAddress);
            return this;
        }

        /// <summary>
        /// 接受人的邮箱地址
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <returns></returns>
        public UseEmail To(string email)
        {
            return To(new MailboxAddress("", email));
        }

        /// <summary>
        /// 接受人的邮箱地址
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <returns></returns>
        public UseEmail To(List<string> email)
        {
            email.ForEach(x =>
            {
                _MailMessage.To.Add(new MailboxAddress("", x));
            });
            return this;
        }

        /// <summary>
        /// 抄送人
        /// </summary>
        /// <param name="mailAddress">抄送人邮箱地址</param>
        /// <returns></returns>
        public UseEmail CC(MailboxAddress mailAddress)
        {
            _MailMessage.Cc.Add(mailAddress);
            return this;
        }

        /// <summary>
        /// 抄送人
        /// </summary>
        /// <param name="email">抄送人邮箱地址</param>
        /// <returns></returns>
        public UseEmail CC(string email)
        {
            return CC(new MailboxAddress("", email));
        }

        /// <summary>
        /// 抄送人多个
        /// </summary>
        /// <param name="email">抄送人邮箱地址多个</param>
        /// <returns></returns>
        public UseEmail CC(List<string> email)
        {
            email.ForEach(x =>
            {
                _MailMessage.Cc.Add(new MailboxAddress("", x));
            });
            return this;
        }

        /// <summary>
        /// 发送邮箱的标题
        /// </summary>
        /// <param name="subject"></param>
        /// <returns></returns>
        public UseEmail Subject(string subject)
        {
            _MailMessage.Subject = subject;
            return this;
        }

        /// <summary>
        /// 发送内容
        /// </summary>
        /// <param name="body">正文</param>
        /// <returns></returns>
        public UseEmail Body(string body)
        {
            var multipart = new Multipart();
            var type = IsBodyHtml == true ? "html" : "plain";
            multipart.Add(new TextPart(type) { Text = body });
            _MailMessage.Body = multipart;
            return this;
        }

    }
}
